package com.suoyue.store.service.impl;

import com.suoyue.store.entity.Report;
import com.suoyue.store.mapper.ReportMapper;
import com.suoyue.store.service.ex.ServiceException;
import com.suoyue.store.service.iReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ReportServiceImpl implements iReportService {
    @Autowired
    ReportMapper reportMapper;
    @Override
    public List<Report> getReportData() {


        List<Report> temp=new ArrayList<Report>();
        List<Report> result=reportMapper.getReportData();
//        System.out.println(result);
        if(result.size()==0){
            for(int i=6;i>=0;i--){
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - i);
                Date today = calendar.getTime();
                Report report=new Report((double) 0,today);
                temp.add(report);
            }
        }
        else{
            int index=0;
            int max=result.size();
            for(int i=6;i>=0;i--){
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - i);
                Date today = calendar.getTime();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                if(index==max){
                    Report report=new Report((double) 0,today);
                    temp.add(report);
                    continue;
                }

                if(format.format(result.get(index).getDate()).equals(format.format(today))){
                    temp.add(result.get(index));
                    index++;
                }
                else{
                    Report report=new Report((double) 0,today);
                    temp.add(report);
                }

            }
        }


        return temp;
    }

    @Override
    public Report getDayReport(String day) {
        List<Report> result=reportMapper.getDayReport(day);
        if(result.size()!=1){
            throw new ServiceException("无对应记录");
        }
        return result.get(0);
    }
}
